﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Hidden_Picture
{
    public partial class Set : Form
    {
        private int[] order = new int[] { 0, 1, 2 };
        private TrackBar[] trackBars = new TrackBar[3];
        private NumericUpDown[] numericUpDowns = new NumericUpDown[3];
        bool mutex = true;
        public Set()
        {
            InitializeComponent();
            trackBars[0] = trackBar1;
            trackBars[1] = trackBar2;
            trackBars[2] = trackBar3;
            numericUpDowns[0] = numericUpDown1;
            numericUpDowns[1] = numericUpDown2;
            numericUpDowns[2] = numericUpDown3;
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
        }
        private void trackBar_Enter(object sender, EventArgs e)
        {
            label1.Focus();
        }

        private void trackBar_Scroll(object sender, EventArgs e)
        {
            if (mutex)
            {
                mutex = false;
                int index;
                if (sender == trackBar1)
                {
                    index = 0;
                }
                else if (sender == trackBar2)
                {
                    index = 1;
                }
                else
                {
                    index = 2;
                }
                SetOrder(index);
                numericUpDowns[index].Value = trackBars[index].Value;
                UpdateValue();
                mutex = true;
            }
            label1.Focus();
            
        }

        private void numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (mutex)
            {
                mutex = false;
                int index;
                if (sender == numericUpDown1)
                {
                    index = 0;
                }
                else if (sender == numericUpDown2)
                {
                    index = 1;
                }
                else
                {
                    index = 2;
                }
                SetOrder(index);
                trackBars[index].Value = (int)numericUpDowns[index].Value;
                UpdateValue();
                mutex = true;
            }
            
        }

        private void SetOrder(int index)
        {
            if (order[0] == index)
            {
                return;
            }
            else if(order[1] == index)
            {
                order[1] = order[0];

            }
            else
            {
                order[2] = order[1];
                order[1] = order[0];
            }
            order[0] = index;
        }

        private void UpdateValue()
        {
            int sum = trackBar1.Value + trackBar2.Value + trackBar3.Value;
            if(sum > 1000)
            {
                sum -= 1000;
                int min = Min(trackBars[order[2]].Value, sum);
                sum -= min;
                trackBars[order[2]].Value -= min;
                numericUpDowns[order[2]].Value -= min;
                if(sum > 0)
                {
                    trackBars[order[1]].Value -= sum;
                    numericUpDowns[order[1]].Value -= sum;
                }
            }
            else
            {
                sum = 1000 - sum;
                int min = Min(1000 - trackBars[order[2]].Value, sum);
                sum -= min;
                trackBars[order[2]].Value += min;
                numericUpDowns[order[2]].Value += min;
                if(sum > 0)
                {
                    trackBars[order[1]].Value += sum;
                    numericUpDowns[order[1]].Value += sum;
                }
            }
        }
        private int Min(int a,int b)
        {
            return a > b ? b : a;
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDown5.Enabled = numericUpDown6.Enabled = radioButton3.Checked;
            int width = (int)numericUpDown5.Value, height = (int)numericUpDown6.Value;
            if(radioButton1.Checked)
            {
                if(Form1.picture1 != null)
                {
                    width = Form1.picture1.Width;
                    height = Form1.picture1.Height;
                }
            }
            else if(radioButton2.Checked)
            {
                if(Form1.picture2 != null)
                {
                    width = Form1.picture2.Width;
                    height = Form1.picture2.Height;
                }
            }
            numericUpDown5.Value = width;
            numericUpDown6.Value = height;
        }

        private void Set_Load(object sender, EventArgs e)
        {

        }
        private void Save()
        {
            PictureIO.R = trackBar1.Value;
            PictureIO.G = trackBar2.Value;
            PictureIO.B = trackBar3.Value;
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    PictureIO.interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
                    break;
                case 1:
                    PictureIO.interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                    break;
                case 2:
                    PictureIO.interpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                    break;
                case 3:
                    PictureIO.interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
                    break;
                case 4:
                    PictureIO.interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic;
                    break;
                case 5:
                    PictureIO.interpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                    break;
                case 6:
                    PictureIO.interpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                    break;
                case 7:
                    PictureIO.interpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    break;
                default:
                    PictureIO.interpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
                    break;
            }
            switch (comboBox2.SelectedIndex)
            {
                case 0:
                    PictureIO.colorMode = PictureIO.ColorMode.GRAY;
                    break;
                case 1:
                    PictureIO.colorMode = PictureIO.ColorMode.BLACK_AND_WHITE;
                    break;
                case 2:
                    PictureIO.colorMode = PictureIO.ColorMode.COLORFUL;
                    break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            order = new int[] { 0, 1, 2 };
            trackBar1.Value = 299;
            trackBar2.Value = 587;
            radioButton1.Checked = true;
            comboBox1.SelectedIndex = 0;
            if(Form1.picture1 == null)
            {
                numericUpDown5.Value = numericUpDown6.Value = 10;
            }
            else
            {
                numericUpDown5.Value = Form1.picture1.Width;
                numericUpDown6.Value = Form1.picture1.Height;
            }
        }

        private void Set_FormClosed(object sender, FormClosedEventArgs e)
        {
            Save();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("id=90743869", "宵宫");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(textBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(textBox2.Text);
        }
    }
}
